package com.yanu.trace.t2s.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yanu.trace.t2s.entity.Pork;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author whj
 * @since 2021-06-23
 */
public interface PorkMapper extends BaseMapper<Pork> {
    /**
     * 联合pork表和enterprise表 返回所有pork实体带上enterprise信息   并分页返回
     * @param page
     * @return
     */
    @Select("select p.*,e.* from pork p,enterprise e where p.enter_id = e.id ")
    @Results(id = "peMappage", value = {
            @Result(column = "id",property = "id"),
            @Result(column = "enter_id",property = "enter_id"),
            @Result(column = "part_name",property = "part_name"),
            @Result(column = "weight",property = "weight"),
            @Result(column = "cut_date",property = "cut_date"),
            @Result(column = "parent",property = "parent"),
            @Result(column = "level",property = "level"),
            @Result(column = "enter_id", property = "enterprise", one = @One(select = "com.yanu.trace.t2s.mapper.EnterpriseMapper.selectById"))
    })
    IPage<Pork> selectPageVo(Page<?> page);

    /**
     * 根据企业id查询该企业所有猪肉信息 并分页返回
     * @param page
     * @param eid
     * @return
     */
    @Select("select * from pork  where enter_id=#{eid} and level=#{level}")
    IPage<Pork> selectPageVoByeid(Page<?> page,Integer eid,Integer level);

    /**
     * 联立pork和enterprise表 根据level查询未被购买的猪肉并分页返回
     * @param page
     * @param level
     * @return
     */
    @Select("select p.*,e.* from pork p,enterprise e where p.enter_id = e.id and p.level=#{level} and p.buy=0")
    @Results(id = "Mapp1", value = {
            @Result(column = "id",property = "id"),
            @Result(column = "enter_id",property = "enter_id"),
            @Result(column = "part_name",property = "part_name"),
            @Result(column = "weight",property = "weight"),
            @Result(column = "cut_date",property = "cut_date"),
            @Result(column = "parent",property = "parent"),
            @Result(column = "level",property = "level"),
            @Result(column = "enter_id", property = "enterprise", one = @One(select = "com.yanu.trace.t2s.mapper.EnterpriseMapper.selectById"))
    })
    IPage<Pork> selectPageVoBylevel(Page<?> page,Integer level);

    /**
     * 根据猪肉id查询该块猪肉的信息和对应的企业信息
     * @param id
     * @return
     */
    @Select("select p.*,e.* from pork p,enterprise e where p.enter_id = e.id and #{id} =p.id ")
    @Results(id = "peMap", value = {
            @Result(column = "id",property = "id"),
            @Result(column = "enter_id",property = "enter_id"),
            @Result(column = "part_name",property = "part_name"),
            @Result(column = "weight",property = "weight"),
            @Result(column = "cut_date",property = "cut_date"),
            @Result(column = "parent",property = "parent"),
            @Result(column = "level",property = "level"),
            @Result(column = "enter_id", property = "enterprise", one = @One(select = "com.yanu.trace.t2s.mapper.EnterpriseMapper.selectById"))
    })
    Pork selectByidPaE(Integer id);

}
